<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require APPPATH.'libraries/simple_html_dom.php';

class TrailProcess extends CI_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->load->library('HttpPost');
		$this->load->helper('url_helper');
	}
	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 *        http://example.com/index.php/welcome
	 *    - or -
	 *        http://example.com/index.php/welcome/index
	 *    - or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */


	public function traillist($p=1,$cbfy='')
	{
		$dsr='';
		$data['AH_BH']='';
		$data['AH_ZH']='';
		if(isset($_POST['PageNo'])){
			$data['PageNo']=$_POST['PageNo'];
		}else{
			$data['PageNo']=1;
		}
		if(isset($_POST['PageSize'])){
			$data['PageSize']=$_POST['PageSize'];
		}else{
			$data['PageSize']=10;
		}
		if(isset($_POST['AH_NH'])){
			$data['AH_NH']=$_POST['AH_NH'];
		}else{
			$data['AH_NH']=2016;
		}
		if(isset($_POST['AH_ZH'])){
			$data['AH_ZH']=$_POST['AH_ZH'];
		}
		if(isset($_POST['AH_BH'])){
			$data['AH_BH']=$_POST['AH_BH'];
		}
		if(isset($_POST['DSR'])){
			$data['DSR']=$_POST['DSR'];
			$dsr=$_POST['DSR'];
		}
		$searchsel['DSR']=$dsr;
		$searchsel['AH_NH']=$data['AH_NH'];
		$searchsel['AH_ZH']=$data['AH_ZH'];
		$searchsel['AH_BH']=$data['AH_BH'];
		$url='http://www.zjsfgkw.cn/TrialProcess/TrialProcessSearchList';
		$timeout = 40;
		$response=$this->httppost->post($url,$data,$timeout);
		$sdlist=json_decode($response);

	//	print_r($sdlist->list);

		$articles=array('title'=>'案件信息查询');
		$articles['trails']=$sdlist;
		$articles['searchsel']=$searchsel;
		if(empty($this->input->post('isAjax'))){
			$this->load->view('head_sec',$articles);
			$this->load->view('trail_process_list',$articles);
			$this->load->view('foot_sec',$articles);
		}else{
			header('content-type:application:json;charset=utf8');
			header('Access-Control-Allow-Origin:*');
			header('Access-Control-Allow-Methods:POST');
			header('Access-Control-Allow-Headers:x-requested-with,content-type');
			$result = array();
			$result['isSuccess'] = true;
			$result['errorMessage'] = '';
			$result['data'] = $sdlist;
			echo json_encode($result, JSON_UNESCAPED_UNICODE);
		}

	}

	public function detail(){
		$url="http://www.zjsfgkw.cn/TrialProcess/TrialProcessSearch";
		$AH=$this->input->post('AH');
		$CXMM=$this->input->post('CXMM');
		$param=array("AH"=>$AH,
			'CXMM'=>$CXMM
		);
		$timeout = 40;
		$response = $this->httppost->post($url, $param, $timeout);
		$html=str_get_html($response);
		$dom = new simple_html_dom();
		$dom->load($html);
		//$table=$dom->find('.dept_basic');
		$table=$dom->find('table[class=dept_basic]', 0);
		$info_array=array();
		$info_title_array=array();
		if(!empty($table)){
			$trs=$table->find('tr');
			$block_num=0;
			$info_array_kv=array();
			foreach ($trs as $tr):
				$th=$tr->find('th',0);
				if(!empty($th)){
					$block_num++;
					$info_title_array[$block_num]=$th->innertext;
					$info_array_kv=array();
				}else{
					$tds=$tr->find('td');
					$tds_num=count($tds);
					$tds_num=$tds_num/2;

					for($i = 0; $i < $tds_num; $i++){
						$arrpair=array();
						array_push($arrpair,$tds[$i * 2]->innertext);
						array_push($arrpair,$tds[$i * 2+1]->innertext);
						array_push($info_array_kv,$arrpair);
					}
					$info_array[$block_num]=$info_array_kv;
				}
			endforeach;
		}
		$data['title']='案件详情';
		$data['info_array']=$info_array;
		$data['info_title_array']=$info_title_array;
		$this->load->view('head_sec',$data);
		$this->load->view('trail_process_detail',$data);
		$this->load->view('foot_sec',$data);
	}
}


